package com.oracle.zebrafreelytraveling.ui.fragment;

import android.app.Activity;
import android.app.FragmentManager;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.oracle.zebrafreelytraveling.R;
import com.oracle.zebrafreelytraveling.data.RequestResult.ResViewSpotIntro;
import com.oracle.zebrafreelytraveling.data.RequestResult.ResViewSpotList;
import com.oracle.zebrafreelytraveling.data.model.IntroBase;
import com.oracle.zebrafreelytraveling.net.HttpManager;
import com.oracle.zebrafreelytraveling.utils.ToastUtil;
import com.oracle.zebrafreelytraveling.utils.constant.Constant;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by yydcnjjw on 2017/8/5.
 */

public class IntroSlideDialogFragment extends SlideBottomDialogFragment implements View.OnClickListener {

    public static final String INTRO_INFO = "introInfo";
    public static final String SID = "sid";
    public static final String INTRO_SLIDE_DIALOGFRAGMENT = "IntroSlideDialogFragment";

    private IntroBase mIntroInfo;
    private int sid;

    private TextView mIntroName;
    private ImageView mIntroClose;
    private SimpleDraweeView mIntroImage;
    private TextView mIntroContent;
    private TextView mIntroLocation;
    private TextView mIntroContact;

    private Activity mActivity;
    private Context mContext;

    public static IntroSlideDialogFragment newInstance(IntroBase info, int sid) {
        IntroSlideDialogFragment f = new IntroSlideDialogFragment();
        Bundle bundle = new Bundle();
        bundle.putSerializable(INTRO_INFO, info);
        bundle.putInt(SID, sid);
        f.setArguments(bundle);
        return f;
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        mContext = context;
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        mActivity = activity;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);
        getBundleData();
        initView();
        initDisplay();
        if (sid != -1){
            getViewSpotIntroInfo(sid);
        }
        return mView;
    }

    private void getBundleData(){
        Bundle args = getArguments();
        mIntroInfo = (IntroBase) args.getSerializable(INTRO_INFO);
        sid = args.getInt(SID);
    }

    private void initView(){
        mIntroName = (TextView) mView.findViewById(R.id.intro_name);
        mIntroClose = (ImageView) mView.findViewById(R.id.intro_close);
        mIntroImage = (SimpleDraweeView) mView.findViewById(R.id.intro_image);
        mIntroContent = (TextView) mView.findViewById(R.id.intro_content);
        mIntroLocation = (TextView) mView.findViewById(R.id.intro_location);
        mIntroContact = (TextView) mView.findViewById(R.id.intro_contact);

        mIntroClose.setOnClickListener(this);
    }

    public void getViewSpotIntroInfo(int sid) {
        List<ResViewSpotIntro.ResultBean> list = new ArrayList<>();
        HttpManager.getInstance().getService(Constant.VIEW_API)
                .getViewSpotIntro(sid)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<ResViewSpotIntro>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {

                    }
                    @Override
                    public void onNext(@NonNull ResViewSpotIntro resViewSpotIntro) {
                        if (resViewSpotIntro.getError_code() == Constant.API_SUCCESS) {
                            list.addAll(resViewSpotIntro.getResult());
                        } else {
                            ToastUtil.showToast(resViewSpotIntro.getReason());
                        }
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        Log.d("getViewSpotIntroInfo", e.getMessage());
                    }

                    @Override
                    public void onComplete() {
                        ResViewSpotIntro.ResultBean viewIntro = list.size() > 0 ? list.get(0) : null;
                        if (viewIntro != null) {
                            mIntroInfo.setIntroContent(viewIntro.getTitle());
                            mIntroInfo.setIntroContact("");
                            mIntroContent.setText(viewIntro.getTitle());
                            mIntroContact.setText("");
                        }
                    }
                });
        //TODO 模拟数据
        mIntroInfo.setIntroContent("好烦还要写这玩意好烦还要写这玩意好烦还要写这" +
                "玩意好烦还要写这玩意好烦还要写这玩意好烦还" +
                "要写这玩意好烦还要写这玩意好烦还要写这玩意好烦还要写这玩意");
        mIntroInfo.setIntroContact("1212121212121212");
        mIntroContent.setText(mIntroInfo.getIntroContent());
        mIntroContact.setText(mIntroInfo.getIntroContact());
    }


    /**
     * 初始化显示
     */
    private void initDisplay(){
        mIntroName.setText(mIntroInfo.getIntroName());
        mIntroImage.setImageURI(mIntroInfo.getIntroImage());
        mIntroLocation.setText(mIntroInfo.getIntroLocation());
        if (mIntroInfo.getIntroContact() != null && mIntroInfo.getIntroContent() != null){
            mIntroContent.setText(mIntroInfo.getIntroContent());
            mIntroContact.setText(mIntroInfo.getIntroContact());
        }
    }

    public void showDialogFragment(FragmentManager fragmentManager){
        show(fragmentManager, INTRO_SLIDE_DIALOGFRAGMENT);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_dialog_intro_slide;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.intro_close:
                dismissAllowingStateLoss();
                break;
        }
    }
}
